/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;


import dao.DanhMucDAO;
import dao.NhapHangDAO;
import dao.SanPhamDAO;
import java.text.*;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.DanhMuc;
import pojo.NhapHang;
import pojo.SanPham;

/**
 *
 * @author OSIRIS
 */
public class SanPhamFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form SanPhamFrame
     */
    public SanPhamFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSanPham = new javax.swing.JTable();
        jBtnThem = new javax.swing.JButton();
        jBtnXoa = new javax.swing.JButton();
        jBtnCapNhat = new javax.swing.JButton();
        jBtnNhapHang = new javax.swing.JButton();

        setClosable(true);
        setTitle("Danh Sách Sản Phẩm");
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jTableSanPham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableSanPham);

        jBtnThem.setText("Thêm");
        jBtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemActionPerformed(evt);
            }
        });

        jBtnXoa.setText("Xóa");
        jBtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnXoaActionPerformed(evt);
            }
        });

        jBtnCapNhat.setText("Cập Nhật");
        jBtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCapNhatActionPerformed(evt);
            }
        });

        jBtnNhapHang.setText("Nhập Hàng");
        jBtnNhapHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNhapHangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jBtnThem)
                .addGap(53, 53, 53)
                .addComponent(jBtnXoa)
                .addGap(73, 73, 73)
                .addComponent(jBtnCapNhat)
                .addGap(45, 45, 45)
                .addComponent(jBtnNhapHang)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnThem)
                    .addComponent(jBtnXoa)
                    .addComponent(jBtnCapNhat)
                    .addComponent(jBtnNhapHang))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void load(){
          // List<SanPham> ds=SanPhamDAO.getList();
        SanPhamDAO spDao = new SanPhamDAO();
        List ds = spDao.findAll();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("ID");
        dtm.addColumn("Sku");
        dtm.addColumn("Ma Danh Muc");
        dtm.addColumn("Ten San Pham");
        dtm.addColumn("Gia Ban");
        dtm.addColumn("Kick Co");
        dtm.addColumn("Mau Sac");
        dtm.addColumn("So Luong");
        for (int i = 0; i < ds.size(); i++) {
            SanPham dm = (SanPham) ds.get(i);
            Vector vt = new Vector();
            vt.add(dm.getId());
            vt.add(dm.getSku());
            vt.add(dm.getDanhMuc().getId());
            vt.add(dm.getTenSanPham());
            vt.add(dm.getGiaBan());
            vt.add(dm.getKickCo());
            vt.add(dm.getMauSac());
            vt.add(dm.getSoLuong());
            dtm.addRow(vt);
        }
        this.jTableSanPham.setModel(dtm);
    }
    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        // TODO add your handling code here:
        load();
      
    }//GEN-LAST:event_formAncestorAdded
private int getSpIdToTable() {
        int selectedRowIndex = jTableSanPham.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        int selectedObject = (Integer) jTableSanPham.getModel().getValueAt(selectedRowIndex, 0);
        return selectedObject;
    }
    private void jBtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemActionPerformed
        // TODO add your handling code here:
        try {
            SanPhamThemDialog dlg = new SanPhamThemDialog(null, true);
            dlg.setTitle("Thêm");
            dlg.setVisible(true);
            SanPhamDAO dmDAO = new SanPhamDAO();
            SanPham info = new SanPham();
            info=dlg.getInfo();
            dmDAO.create(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnThemActionPerformed

    private void jBtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnXoaActionPerformed
        // TODO add your handling code here:
         try {
            SanPhamDAO infoDAO = new SanPhamDAO();
            SanPham info = new SanPham();
            info.setId(this.getSpIdToTable());
            infoDAO.delete(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnXoaActionPerformed

    private void jBtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCapNhatActionPerformed
        // TODO add your handling code here:
         try {
            SanPhamThemDialog dlg = new SanPhamThemDialog(null, true);
            dlg.setTitle("Cập Nhật");
            SanPham info=new SanPham();
            SanPhamDAO infoDao=new SanPhamDAO();
            info =infoDao.find(this.getSpIdToTable());
            dlg.setInfo(info);
            dlg.setVisible(true);
            info=dlg.getInfo();
            infoDao.update(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnCapNhatActionPerformed

    private void jBtnNhapHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNhapHangActionPerformed
        // TODO add your handling code here:
         try {
            NhapHangThemDialog dlg = new NhapHangThemDialog(null, true);
            dlg.setTitle("Thêm");
            int idSp=this.getSpIdToTable();
            Short id=Short.parseShort(String.valueOf(idSp));
            dlg.setIdSanPham(id);
            dlg.setVisible(true);
            SanPhamDAO spDao=new SanPhamDAO();
        NhapHangDAO nhDao=new NhapHangDAO();
        SanPham sp=new SanPham();
        NhapHang nh=new NhapHang();
        Short soLuong=dlg.getSoLuong();
        Double giaNhap=dlg.getGiaNhap();
       
        
       
        Date date = new Date();

       nh.setIdSanpham(id);
       nh.setSoLuong(soLuong);
       nh.setGiaNhap(giaNhap);
       nh.setNgayNhap(date);
       nhDao.create(nh);
       
       sp=spDao.find(idSp);
       Short soLuongCu=sp.getSoLuong();
       int  soLuongMoi=soLuong+soLuongCu;
       soLuong=Short.parseShort(String.valueOf(soLuongMoi));
       sp.setSoLuong(soLuong);
       spDao.update(sp);
       load();
         } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnNhapHangActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCapNhat;
    private javax.swing.JButton jBtnNhapHang;
    private javax.swing.JButton jBtnThem;
    private javax.swing.JButton jBtnXoa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableSanPham;
    // End of variables declaration//GEN-END:variables
}
